-- EffectCacheM
-- create by zouyb
-- 特效缓存模块
-- 缓存常用的特效，防止创建节点导致性能下降

module("EffectCacheM", package.seeall);

local effectNames = {};
local effectNodes = {};
local effectActions = {};

-- 获取特效
function createEffectNodeAndAction(effectName)
    if effectNodes[effectName] == nil then
        effectNodes[effectName] = {};
        effectActions[effectName] = {};
    end

    -- 缓冲池里面有，直接取出来用
	if #effectNodes[effectName] > 0 and #effectActions[effectName] > 0 then
        local node = effectNodes[effectName][1];
        table.removeItem(effectNodes[effectName], node);
        
        local action = effectActions[effectName][1]; 	
        table.removeItem(effectActions[effectName], action);   
        return node, action;
	end

	local node = cc.CSLoader:createNode("effects/" .. effectName);
	node:retain();
	
	local action = cc.CSLoader:createTimeline("effects/" .. effectName);
	action:retain();
	
    return node, action;
end

-- 释放特效
function freeEffectNodeAndAction(effectName, node, action)
    -- 存到缓存中
    table.insert(effectNodes[effectName], node);
    table.insert(effectActions[effectName], action);
    table.insert(effectNames, effectName);
end

-- 清空缓存
function purgeCache()
    for _, name in pairs(effectNames) do
        for _, node in pairs(effectNodes[name]) do
            node:release();
        end
        
        for _, action in pairs(effectActions[name]) do
            action:release();
        end        
    end
    
    effectNames = {};
    effectNodes = {};
    effectActions = {};    
end

